const mysql = require('mysql2');
import skill from '../db/skill';

const defaultInfo = {
  Action: 0,
  Area: 0,
  Limits: 0,
  Ratio: 0,
  Round: 0,
  Additional: 0,
  Logic1: 0,
  Logic1_Type: 0,
  Logic1_Parameter: 0,
  Logic1_Round: 0,
  Logic2: 0,
  Logic2_Type: 0,
  Logic2_Parameter: 0,
  Logic2_Round: 0,
  Logic3: 0,
  Logic3_Type: 0,
  Logic3_Parameter: 0,
  Logic3_Round: 0,
};

const getSkillType = (name: string) => {
  if (name === '对第一排所有敌人造成伤害。') {
    return {
      ...defaultInfo,
      Action: 1,
      Area: 3,
      Limits: 1,
      Ratio: 1,
    };
  }
  if (name === '对前两排所有敌人造成伤害。') {
    return {
      ...defaultInfo,
      Action: 1,
      Area: 6,
      Limits: 1,
      Ratio: 2,
    };
  }
  if (name === '对所有敌人造成伤害。') {
    return {
      ...defaultInfo,
      Action: 1,
      Area: 9,
      Limits: 1,
      Ratio: 3,
    };
  }
  if (name === '对第一排单体敌人造成伤害。') {
    return {
      ...defaultInfo,
      Action: 1,
      Area: 1,
      Limits: 1,
      Ratio: 4,
    };
  }
  if (name === '对前方最后一名敌人造成伤害。') {
    return {
      ...defaultInfo,
      Action: 1,
      Area: 1,
      Limits: 3,
      Ratio: 5,
    };
  }
  if (name === '对直线范围内所有敌人造成伤害。') {
    return {
      ...defaultInfo,
      Action: 1,
      Area: 3,
      Limits: 2,
      Ratio: 6,
    };
  }
  if (name === '对十字范围内所有敌人造成伤害。') {
    return {
      ...defaultInfo,
      Action: 1,
      Area: 5,
      Limits: 7,
      Ratio: 7,
    };
  }
  if (name === '增加我方所有角色战意值。') {
    return {
      ...defaultInfo,
      Action: 4,
      Area: 9,
      Limits: 1,
      Ratio: 8,
    };
  }
  if (name === '恢复我方所有角色生命值。') {
    return {
      ...defaultInfo,
      Action: 2,
      Area: 9,
      Limits: 1,
      Ratio: 9,
    };
  }
  if (name === '降低所有敌人攻击力持续一回合。') {
    return {
      ...defaultInfo,
      Action: 3,
      Area: 9,
      Limits: 1,
      Ratio: 10,
    };
  }
  if (name === '为我方生命值最少的单个角色恢复生命值。') {
    return {
      ...defaultInfo,
      Action: 2,
      Area: 1,
      Limits: 4,
      Ratio: 11,
    };
  }
  if (name === '增加我方所有角色闪避持续一回合。') {
    return {
      ...defaultInfo,
      Action: 4,
      Area: 9,
      Limits: 1,
      Ratio: 12,
    };
  }
  return {
    ...defaultInfo,
    Action: 999,
  };
};

const datas = skill.map(v => {
  return {
    ...v,
    ...getSkillType(v.Desc),
  };
});

const connection = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: 'Zhao5162300..0',
  database: 'xxsg_db',
});

connection.connect((err: any) => {
  if (err) {
    console.log(err);
    return;
  }

  console.log('数据库连接成功');
  const insertQuery =
    'INSERT INTO config_skill (id, name, description, type, action, area, limits, ratio, round, additional, logic1, logic1_type, logic1_parameter, logic1_round, logic2, logic2_type, logic2_parameter, logic2_round, logic3, logic3_type, logic3_parameter, logic3_round) VALUES ?';

  connection.query(
    insertQuery,
    [datas.map((data: any) => Object.values(data))],
    (err: any, result: { affectedRows: any }) => {
      if (err) {
        console.error('插入数据失败:', err);
        return;
      }

      console.log(`插入 ${result.affectedRows} 条数据.`);
    },
  );

  // 关闭连接
  connection.end();
});
